/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Order.java
 *
 * Created on Dec 10, 2009, 11:20:49 AM
 */

package GUI;

import Business.Bill_BS;
import Business.BillProcess_BS;
import Business.loadBill;
import DAO.Bill_DAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author trangnn_b00557
 */
public class Bill extends javax.swing.JPanel {

    /** Creates new form Order */
    public static int cID;
    private BillProcess_BS op = BillProcess_BS.getOrder();
    private loadBill load;
    public Bill() {
        initComponents();
        try {
            load = op.viewOrder();
            tbOrderList.setModel(load);
        } catch (Exception e) {
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnFIilterOrder = new javax.swing.JPanel();
        pnOrder = new javax.swing.JPanel();
        lblCreateOrderDate = new javax.swing.JLabel();
        txtCreateOrderDate1 = new javax.swing.JTextField();
        lbTo = new javax.swing.JLabel();
        txtCreateOrderDate2 = new javax.swing.JTextField();
        lblPrice = new javax.swing.JLabel();
        txtPrice1 = new javax.swing.JTextField();
        lbTo1 = new javax.swing.JLabel();
        txtPrice2 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        btSearch = new javax.swing.JButton();
        btRefresh = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbOrderList = new javax.swing.JTable();
        btCreate = new javax.swing.JButton();
        btDelete = new javax.swing.JButton();
        btClose = new javax.swing.JButton();

        pnFIilterOrder.setBorder(javax.swing.BorderFactory.createTitledBorder("Filter"));

        pnOrder.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Order", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.TOP));

        lblCreateOrderDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblCreateOrderDate.setText("Create Order Date :");

        lbTo.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbTo.setText("To");

        lblPrice.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblPrice.setText("Price : ");

        lbTo1.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbTo1.setText("To");

        javax.swing.GroupLayout pnOrderLayout = new javax.swing.GroupLayout(pnOrder);
        pnOrder.setLayout(pnOrderLayout);
        pnOrderLayout.setHorizontalGroup(
            pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnOrderLayout.createSequentialGroup()
                .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnOrderLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblCreateOrderDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnOrderLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblPrice)
                        .addGap(35, 35, 35)))
                .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtPrice1)
                    .addComponent(txtCreateOrderDate1, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnOrderLayout.createSequentialGroup()
                        .addComponent(lbTo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                        .addComponent(txtCreateOrderDate2, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnOrderLayout.createSequentialGroup()
                        .addComponent(lbTo1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPrice2, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnOrderLayout.setVerticalGroup(
            pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnOrderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnOrderLayout.createSequentialGroup()
                        .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtCreateOrderDate1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbTo)
                            .addComponent(txtCreateOrderDate2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtPrice1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblPrice)
                            .addComponent(lbTo1)
                            .addComponent(txtPrice2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblCreateOrderDate))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Customer", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.TOP));

        lblName.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblName.setText("Name : ");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(lblName)
                .addGap(24, 24, 24)
                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Filter.png"))); // NOI18N
        btSearch.setText("Search");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        btRefresh.setText("Refresh");
        btRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRefreshActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnFIilterOrderLayout = new javax.swing.GroupLayout(pnFIilterOrder);
        pnFIilterOrder.setLayout(pnFIilterOrderLayout);
        pnFIilterOrderLayout.setHorizontalGroup(
            pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                .addComponent(pnOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(btSearch)
                        .addGap(18, 18, 18)
                        .addComponent(btRefresh))
                    .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        pnFIilterOrderLayout.setVerticalGroup(
            pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                .addGroup(pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnFIilterOrderLayout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(pnFIilterOrderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btSearch)
                            .addComponent(btRefresh)))
                    .addComponent(pnOrder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Order List"));

        tbOrderList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbOrderList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbOrderListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbOrderList);

        btCreate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Add.png"))); // NOI18N
        btCreate.setText("Create");
        btCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCreateActionPerformed(evt);
            }
        });

        btDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Delete-icon.png"))); // NOI18N
        btDelete.setText("Delete");
        btDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteActionPerformed(evt);
            }
        });

        btClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Close-icon.png"))); // NOI18N
        btClose.setText("Close");
        btClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(280, Short.MAX_VALUE)
                .addComponent(btCreate)
                .addGap(33, 33, 33)
                .addComponent(btDelete)
                .addGap(31, 31, 31)
                .addComponent(btClose)
                .addGap(34, 34, 34))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btCreate)
                    .addComponent(btClose)
                    .addComponent(btDelete)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnFIilterOrder, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnFIilterOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCreateActionPerformed
        // TODO add your handling code here:
        CreateBill order= new CreateBill(null, true);
        order.setVisible(true);
//        CreatePurchase purchase= new CreatePurchase(null, true);
//        purchase.setVisible(true);
    }//GEN-LAST:event_btCreateActionPerformed

    private void btDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this Bill?")
                == JOptionPane.YES_OPTION) {
            int row = tbOrderList.getSelectedRow();
            Bill_BS b = getOrderAtRow(row);
            Bill_DAO.deletOrder(b.getBID());

            ((loadBill) tbOrderList.getModel()).deleteOrder(b);
        }
    }//GEN-LAST:event_btDeleteActionPerformed

    private void btCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCloseActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_btCloseActionPerformed

    private void btRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRefreshActionPerformed
        // TODO add your handling code here:
         txtCreateOrderDate1.setText("");
        txtCreateOrderDate2.setText("");
        txtName.setText("");
        txtPrice1.setText("");
        txtPrice2.setText("");
    }//GEN-LAST:event_btRefreshActionPerformed

    private void tbOrderListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbOrderListMouseClicked
        // TODO add your handling code here:
         if (evt.getClickCount() > 1) {
            int row = tbOrderList.getSelectedRow();
            Bill_BS selectedOrder = getOrderAtRow(row);

            BillDetails order = new BillDetails(null, true);
            order.setOrder(selectedOrder);
            order.setVisible(true);
           // pid = Integer.valueOf(tbPurchaseList.getValueAt(tbPurchaseList.getSelectedRow(), 0).toString());

            //((PurchaseDetailsTableModel) tbPurchaseList.getModel()).updateCustomer(selectedCustomer);
        }
    }//GEN-LAST:event_tbOrderListMouseClicked

    private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
        // TODO add your handling code here:
        Bill_DAO o = new Bill_DAO();
        try {
            String cusName = txtName.getText();

            float price1 = (txtPrice1.getText().length() == 0) ?
                -1 : Float.parseFloat(txtPrice1.getText());
            float price2 = txtPrice2.getText().length() == 0 ?
                -1 : Float.parseFloat(txtPrice2.getText());
            Date date1 = null;
            Date date2 = null;

            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
            if (!txtCreateOrderDate1.getText().isEmpty())
                date1 = format.parse(txtCreateOrderDate1.getText());

            if (!txtCreateOrderDate2.getText().isEmpty())
            date2 = format.parse(txtCreateOrderDate2.getText());

            List<Bill_BS> order = o.searchOrder(cusName,price1, price2,date1,date2);
            tbOrderList.setModel( new loadBill(order));
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btSearchActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btClose;
    private javax.swing.JButton btCreate;
    private javax.swing.JButton btDelete;
    private javax.swing.JButton btRefresh;
    private javax.swing.JButton btSearch;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbTo;
    private javax.swing.JLabel lbTo1;
    private javax.swing.JLabel lblCreateOrderDate;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JPanel pnFIilterOrder;
    private javax.swing.JPanel pnOrder;
    private javax.swing.JTable tbOrderList;
    private javax.swing.JTextField txtCreateOrderDate1;
    private javax.swing.JTextField txtCreateOrderDate2;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPrice1;
    private javax.swing.JTextField txtPrice2;
    // End of variables declaration//GEN-END:variables
 private Bill_BS getOrderAtRow(int row) {
        loadBill model = (loadBill) tbOrderList.getModel();
        return model.getOrderBS(row);
    }
}
